package com.web.my12306.dao.impl;

import com.web.my12306.dao.IAdminDao;
import com.web.my12306.dao.impl.BaseDao;
import com.web.my12306.pojo.Admin;
import com.web.my12306.pojo.User;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AdminDaoImpl extends BaseDao implements IAdminDao
{
    /*
    * 耿洋
    *
    * */
    @Override
    //管理员登录
    public Admin Login(String loginname, String password) {
        String sql = "select * from admin where loginname=? and password=?";
        query(sql,loginname,password);
        Admin ad = null;
        try {
            while (rs.next()){
                ad = new Admin();
                ad.setAid(rs.getInt("aid"));
                ad.setLoginname(rs.getString("loginname"));
                ad.setPassword(rs.getString("password"));
                ad.setRoleid(rs.getInt("roleid"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ad;
    }

    //新增管理员
    @Override
    public void addAdmin(String loginname, String password,int roleid) {
        String sql = "insert into admin(loginname,password,roleid) values(?,?,?) ";
        update(sql, loginname, password,roleid);
    }

    @Override
    //genju
    public Admin findByName(String loginname) {
        String sql = "select * from admin where loginname=?";
        query(sql,loginname);
        Admin ad_1 = null;
        try {
            while (rs.next()){
                ad_1 = new Admin();
                ad_1.setAid(rs.getInt("aid"));
                ad_1.setLoginname(rs.getString("loginname"));
                ad_1.setPassword(rs.getString("password"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ad_1;
    }

    @Override
    //根据工号修改密码
    public void editPassword(String loginname, String password) {
        String sql = "update admin set password=? where loginname=?";
        update(sql,password,loginname);
    }

    @Override
    //查询所有管理员
    public List<Admin> findAll() {
        List<Admin> list = new ArrayList<Admin>();
        String sql = "select * from admin";
        query(sql);
        Admin ad_2 = null;
        try {
            while (rs.next()){
                ad_2 = new Admin();
                ad_2.setAid(rs.getInt("Aid"));
                ad_2.setLoginname(rs.getString("loginname"));
                ad_2.setPassword(rs.getString("password"));
                ad_2.setRoleid(rs.getInt("roleid"));
                list.add(ad_2);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<User> findAllUser() {
        List<User> list = new ArrayList<>();
        String sql = "select * from user";
        query(sql);
        User user = null;
        try {
            while (rs.next()){
                user = new User();
                user.setUid(rs.getInt("uid"));
                user.setLoginname(rs.getString("loginname"));
                user.setPassword(rs.getString("password"));
                user.setPasswordold(rs.getString("passwordold"));
                user.setImg(rs.getString("img"));
                user.setName(rs.getString("name"));
                user.setSex(rs.getString("sex"));
                user.setProv(rs.getString("prov"));
                user.setCity(rs.getString("city"));
                user.setCardtype(rs.getString("cardtype"));
                user.setCardid(rs.getString("cardid"));
                user.setTel(rs.getString("tel"));
                user.setUtype(rs.getString("utype"));
                user.setBeizhu(rs.getString("beizhu"));
                user.setState(rs.getInt("state"));
                list.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public void delete(String loginname) {
        String sql = "delete * from admin where loginname=?";
        update(sql,loginname);
    }
}
